<?php

App::uses('FormHelper', 'View/Helper');

class TinyMceHelper extends AppHelper
{

    public $helpers = array('Form', 'Html');
    private $_MceAdded = false;

    public function tinyMce($model = null, $options = array())
    {
        $this->Html->script('tiny_mce/tiny_mce', array('inline' => false));
        $textareaId = $this->Html->domId($model) . $this->getID();
        $options = Set::merge(array('type' => 'textarea', 'id' => $textareaId, 'class' => ''), $options);

        $options['class'] .= ' tinymcetransform mce-' . $textareaId;

        if (!$this->_MceAdded)
        {
            $this->Html->scriptBlock('
            $(function() {
                tinyMCE.init({
                        mode : "specific_textareas",
                        theme_advanced_path : false,
                        plugins: "autoresize",
                        width: "100%",
                        editor_selector : "tinymcetransform",
                        content_css : "' . Router::url('/', true) . 'css/editor_content.css",
                        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,numlist,bullist,|,link,unlink,|,forecolor,|,code",
                });   
                tinyMCE.execCommand("mceAddControl", false, "tinymcetransform");
            });
            ', array('inline' => false));

            $this->_MceAdded = true;
        }

        return $this->Form->input($model, $options);
    }

}